import React from 'react';
import { createFromIconfontCN } from '@ant-design/icons';

const IconExt = createFromIconfontCN({
  scriptUrl: '//at.alicdn.com/t/font_694023_mxroambmce.js',
  extraCommonProps: { theme: 'outlined' },
  ...process.env.ICON_FONT,
});

/**
 * import 编译时调用, require 运行时调用
 */
const allIcons = require('@ant-design/icons/es/icons');

function formatName(name) {
  const newName = `${name
    .replace(name[0], name[0].toUpperCase())
    .replace(/-(\w)/g, (all, letter) => {
      return letter.toUpperCase();
    })}Outlined`;
  return newName;
}

const Index = (props) => {
  const { type, ...rest } = props;

  return type.startsWith('icon-') ? (
    <IconExt {...rest} type={type} />
  ) : (
    React.createElement(allIcons[type] || allIcons[formatName(type)], rest)
  );
};

Index.displayName = 'Icon';
export default Index;
